package com.senven.goods.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.senven.common.entity.Goods;
import com.senven.goods.Vo.GoodsVo;
import com.senven.goods.service.IGoodsService;
import com.senven.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gjh
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/goods/goods")
@Api(value = "后台菜品信息")
public class GoodsController {

    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @ApiOperation(value="根据分页显示所有的菜品信息")
    @PostMapping("/selectAll")
    public CommonResult selectAll(HttpServletRequest request, Integer currentPage, Integer pageSize,@RequestBody Goods goodsVo ){
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");
        System.out.println(goodsVo);
        return  iGoodsService.selectall(id,currentPage,pageSize,goodsVo);


    }
    @ApiOperation(value="添加菜品信息")
    @PostMapping("/shopsAdd")

    public CommonResult shopsAdd(@RequestBody Goods goods,HttpServletRequest request){
        System.out.println(goods);
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");

        return iGoodsService.shopsAdd(goods,id);
    }

    @ApiOperation(value="删除菜品信息")
    @PostMapping("/goodsDel")
    public CommonResult goodsDel( Integer goodsid,HttpServletRequest request){
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");
        return iGoodsService.goodsDel(goodsid,id);
    }

    @ApiOperation(value="修改菜品信息")
    @PostMapping("/goodsUpdate")
    public CommonResult goodsUpdate(@RequestBody Goods goods,HttpServletRequest request){
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");
        System.out.println(goods);
        return  iGoodsService.goodsUpdate(goods,id);
    }
//显示所有的种类信息
    @GetMapping("/selectType")
    public CommonResult selectType() {
        return iGoodsService.selectType();
    }
//查询所有的管理员权限
        @GetMapping("/selectRole")
        public CommonResult selectRole(HttpServletRequest request){
            String token = request.getHeader("token");
            String s = redisTemplate.opsForValue().get(token);
            Map map = JSON.parseObject(s, Map.class);
            JSONObject user = (JSONObject) map.get("user");
            String id = user.getString("id");
            return  iGoodsService.selectRole(id);

}

}
